package com.igoal.utils;

import java.io.File;
import java.util.List;

/**
 * Utilities regarding File
 * 
 * @author Nam Tran
 * @version 1.0
 */

public class FileUtils {
	
	/**
	 * Add child directory to a given path
	 */
	public static StringBuffer addDir(StringBuffer sbBase, String sDir) {
		if (sbBase == null || sDir == null) {
			return null;
		} else {
			return addDir(sbBase, new StringBuffer(sDir));
		}
	}

	public static StringBuffer addDir(StringBuffer sbBase, StringBuffer sbDir) {
		if (sbBase == null || sbDir == null) {
			return null;
		} else {		
			
			StringBuffer sb = new StringBuffer(sbBase);
			
			if (sbBase.length() != 0 && sbDir.length() != 0) {
				sb.append(File.separator);
			}
			
			sb.append(sbDir);
			return sb;
		}
	}	
	
	public static boolean isAllowableExt(String sFileName, List<String> allowExtensionAccepts){
		if(sFileName == null || allowExtensionAccepts == null){
			return false;
		}
		int dot = sFileName.lastIndexOf('.');
		if(dot < 0){
			return false;
		}
		String ext = sFileName.substring(dot+1, sFileName.length());
		if(ext != null){			
			for (String extAccept : allowExtensionAccepts) {
				if(extAccept.equalsIgnoreCase(ext)){
					return true;
				}
			}			
		}
		return false;
	}
}